/*
 * @Author: Superficial
 * @Date: 2020-11-24 13:26:22
 * @LastEditTime: 2020-12-03 15:25:21
 * @Description: 客户端初始化任务管理器
 */

import { isBrowser } from '~/environment'

if (isBrowser) {
  // 初始化
  const loadedHander = (event) => {
    window.loaded = true
    window.loadedTasks.forEach((task) => doTask(task))
  }
  // 执行任务
  const doTask = (task) => {
    window.setTimeout(task, 888)
  }
  // 任务管理
  const addLoadedTask = (task) => {
    if (window.loaded) {
      doTask(task)
    } else {
      window.loadedTasks.push(task)
    }
  }
  window.loaded = false
  window.loadedTasks = []
  window.addLoadedTask = addLoadedTask
  window.addEventListener('load', loadedHander)
}
